package org.example.singletonPattern;

public class SingletonDemo {
    public static void main(String[] args) {
        // 1. 测试饿汉式
        EagerSingleton eager1 = EagerSingleton.getInstance();
        EagerSingleton eager2 = EagerSingleton.getInstance();
        eager1.showMessage();
        System.out.println("饿汉式单例是否相同: " + (eager1 == eager2));

        // 2. 测试懒汉式（线程不安全）
        LazySingleton lazy1 = LazySingleton.getInstance();
        LazySingleton lazy2 = LazySingleton.getInstance();
        lazy1.showMessage();
        System.out.println("懒汉式单例是否相同: " + (lazy1 == lazy2));

        // 3. 测试双重检查锁
        DoubleCheckedSingleton dcl1 = DoubleCheckedSingleton.getInstance();
        DoubleCheckedSingleton dcl2 = DoubleCheckedSingleton.getInstance();
        dcl1.showMessage();
        System.out.println("双重检查锁单例是否相同: " + (dcl1 == dcl2));

        // 4. 测试静态内部类
        InnerClassSingleton inner1 = InnerClassSingleton.getInstance();
        InnerClassSingleton inner2 = InnerClassSingleton.getInstance();
        inner1.showMessage();
        System.out.println("静态内部类单例是否相同: " + (inner1 == inner2));

        // 5. 测试枚举
        EnumSingleton enum1 = EnumSingleton.INSTANCE;
        EnumSingleton enum2 = EnumSingleton.INSTANCE;
        enum1.showMessage();
        System.out.println("枚举单例是否相同: " + (enum1 == enum2));
    }
}